package com.linjj.seckill.mapper;

import com.linjj.seckill.vo.SeckillGoodsVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
@Mapper
public interface GoodsMapper {
    @Select("select g.*,s.seckill_stock, s.seckill_start, s.seckill_end,s.seckill_price from seckill s left join goods g on s.goods_id = g.id")
    public List<SeckillGoodsVo> querySeckills();

    @Select("select g.*,s.seckill_stock, s.seckill_start, s.seckill_end,s.seckill_price from seckill s left join goods g on s.goods_id = g.id where SYSDATE()-s.seckill_start<0")
    public List<SeckillGoodsVo> queryNotStartSeckills();

    @Select("select g.*,s.seckill_stock, s.seckill_start, s.seckill_end,s.seckill_price from seckill s left join goods g on s.goods_id = g.id where SYSDATE()-s.seckill_start>0 and SYSDATE()-s.seckill_end<0")
    public List<SeckillGoodsVo> queryStartedSeckills();

    @Select("select g.*,s.seckill_stock, s.seckill_start, s.seckill_end,s.seckill_price from seckill s left join goods g on s.goods_id = g.id where SYSDATE()-s.seckill_end>0")
    public List<SeckillGoodsVo> queryOverSeckills();

    @Select("select g.*,s.seckill_stock, s.seckill_start, s.seckill_end,s.seckill_price from seckill s left join goods g on s.goods_id = g.id where g.id = #{goodsId}")
    public SeckillGoodsVo querySeckill(long goodsId);
    @Update("update goods set goods_stock = goods_stock - 1 where id = #{goodsId} and goods_stock > 0")
    public int reduceGoodsStock(long goodsId);
    @Update("update seckill set seckill_stock = seckill_stock - 1 where goods_id = #{goodsId} and seckill_stock > 0")
    public int reduceSeckillStock(long goodsId);
}
